from pathlib import Path
from typing import Dict, Optional
from .template_parser import PromptTemplateParser


def load_template(filepath: str | Path) -> PromptTemplateParser:
    """加载提示词模板"""
    file = Path(filepath)
    if not file.exists():
        raise FileNotFoundError

    with open(file, "r", encoding="utf-8") as fs:
        file_content = fs.read()
    template_prompt = PromptTemplateParser(file_content)
    return template_prompt


def load_prompt(filepath: str | Path, inputs: Optional[Dict] = None) -> str:
    """加载提示词"""
    template_prompt = load_template(filepath)
    prompt = template_prompt.format(
        inputs=inputs or {},
        remove_template_variables=True
    )
    return prompt
